package task01;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentManagementSystem {
    public static List<Student> allStudents = new ArrayList<>();
    public final static String FILE_NAME = "src/task01/students_info.txt";

    public boolean login(String password){
        System.out.println("-------正在登录--------");
        if (password.equals("123456")){
            System.out.println("登陆成功");
            readList();
            return true;
        }else {
            System.out.println("登陆失败");
            return false;
        }
    }

    public void logout(){
        writeList();
    }

    public void printALl(){
        System.out.println("------打印所有学生-------");
        for (Student s : allStudents){
            System.out.println(s);
        }
        System.out.println("-----------------------");

    }

    public boolean addStudent(Student student){
        System.out.println("--------添加学生--------");
        boolean success = allStudents.add(student);
        if(success){
            System.out.println(student.getName() + "添加成功");
        }else{
            System.out.println(student.getName() + "添加失败");

        }
        System.out.println("----------------------");
        return success;
    }

    public boolean deleteStudent(String number){
        System.out.println("--------删除学生--------");
        for (int i = 0; i< allStudents.size(); i++){
            if (allStudents.get(i).getId().equals(number)){
                if(allStudents.remove(allStudents.get(i))){
                    System.out.println("删除成功");
                    return true;
                }else{
                    System.out.println("删除失败");
                    return false;
                }

            }
        }
        System.out.println("删除失败");
        return false;
    }


    private static List readList(){
        ObjectInputStream in=null;
        File file = new File(FILE_NAME);
        if(file.length() != 0)
            try {
                in = new ObjectInputStream(new FileInputStream("src/task01/students_info.txt"));
                Object o = in.readObject();
                allStudents =(List) o;

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (null!=in){
                    try {
                        in.close();

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        return allStudents;
    }

    private static void writeList(){
        ObjectOutputStream out=null;
        try {
            out=new ObjectOutputStream(new FileOutputStream(FILE_NAME));
            out.writeObject(allStudents);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }


}
